home *** CD-ROM | disk | FTP | other *** search
- Path: news.vcnet.com!usenet
- From: swedecj@vcnet.com (Carl Jacobson)
- Newsgroups: comp.lang.c
- Subject: Re: hex to dec function?
- Date: Sat, 02 Mar 1996 20:30:22 GMT
- Organization: Internet Access of Ventura County 805.383.3500
- Message-ID: <4hab7t$uo4@news.vcnet.com>
- References: <4gdh1b$bg@mailhost.mwmicro.com> <4gdolr$ha9@madeline.INS.CWRU.Edu> <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> <danpop.824997504@rscernix>
- NNTP-Posting-Host: port12.vcnet.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- danpop@mail.cern.ch (Dan Pop) wrote:
-
- >In <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
-
- >>In article <4gdolr$ha9@madeline.INS.CWRU.Edu>,
- >>Michael A. Balfour <mab22@po.CWRU.Edu> wrote:
- >> >In a previous article, aschlies@citynet.net () says:
- >> >>Hi There,
- >> >>Is there a function that converts HEX to Dec in ANSI C?
- >> >You could try sscanf. For example:
- >> >main()
- >> >{
- >> > char hex[9];
- >> > int dec;
- >> > strcpy(hex,"FFFF");
- >>
- >>Of course, someone will change the above to " FFFFF", thus clobbering
- >>your activation record, since you comitted the hex array to hold only 9
- >>characters.
- >>
- >>There is no reason why you can't use char *hex = "FFFF"; in such a small
- >>example.
-
- >For such a small example, sscanf("FFFF", "%x", &dec); would have done the
- >job :-)
-
- >Note, however, that there are broken implementations where only the original
- >example would work, while Kazimir's and mine would segfault! This
- >is because string literals _may_ be stored in read-only memory segments
- >and some old sscanf implementations attempted to write back (actually
- >to ungetc) the first character that couldn't be converted (the terminating
- >null in this case). A few years ago, somebody actually posted a similar
- >example which crashed on his system and he couldn't understand why.
-
- >Dan
- >--
- >Dan Pop
- >CERN, CN Division
- >Email: danpop@mail.cern.ch
- >Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-
- Why does the following hang my pc when the printf statement is being
- executed. If I put a breakpoint on the printf, watch and reset,
- everything seems OK up to that point. The answer appears to be in the
- above comments, but I do not understand the premise.
-
- char *pref = "0x";
- char *suff;
- long numb;
-
- scanf("%s",suff);
- strcat(pref,suff);
- sscanf(pref,"0x%lx",&numb);
-
- printf(" %ld\n",numb);
-
- TIA
- Carl
- ---------------------------------------------------------------
- Carl Jacobson swedecj@vcnet.com
- P.O. Box 3607 Phone: US 805.523.1724
- Thousand Oaks, CA 91359 Fax : US 805.523.1454
-
-
-